// server.js
const express = require('express');
const multer = require('multer');
const ftp = require('basic-ftp');
const path = require('path');
const fs = require('fs');

const app = express();
const port = 3000;

// 设置 multer 存储位置（临时目录）
const upload = multer({ dest: 'uploads/' });

app.use(express.static('public')); // 假设 HTML 文件在 public 文件夹中

app.post('/upload', upload.single('file'), async (req, res) => {
    if (!req.file) {
        return res.status(400).send('没有上传文件');
    }

    const filePath = req.file.path;
    const fileName = req.file.originalname;

    const client = new ftp.Client();
    client.ftp.verbose = false;

    try {
        await client.access({
            host: 'www.doinm.top',
            port: 22, // 注意：22 通常是 SSH/SFTP 的端口，不是 FTP 默认端口，basic-ftp 不支持 SFTP，只支持 FTP（21端口）
            user: '000000',
            password: '000000',
            secure: false,
        });

        await client.uploadFrom(filePath, fileName);
        res.send('文件上传成功');
    } catch (err) {
        console.error(err);
        res.status(500).send('上传失败');
    } finally {
        client.close();
        fs.unlink(filePath, () => {}); // 删除本地临时文件
    }
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});
